package com.higgins.eventtracking.client.health.fundamentals;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.logging.Logger;

import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.higgins.eventtracking.client.EventTrackingService;
import com.higgins.eventtracking.client.EventTrackingServiceAsync;
import com.higgins.eventtracking.client.dto.HealthAlertDTO;
import com.higgins.eventtracking.client.dto.TimeTableTaskDTO;
import com.higgins.eventtracking.client.genericcomponents.GenericColumn;
import com.higgins.eventtracking.client.genericcomponents.GenericColumns;
import com.higgins.eventtracking.client.genericcomponents.GenericTable;

public class HolidayTable extends GenericTable	{
	private static final Logger log = Logger.getLogger(HolidayTable.class.getName());
	private final EventTrackingServiceAsync greetingService = GWT
			.create(EventTrackingService.class);
	private Date storedDate = new Date();
	private Integer holidayUnit = null;
	
	public HolidayTable(GenericColumns genericColumns) {
		super(genericColumns);
		greetingService.getWeekNumber(new AsyncCallback<Integer>() {
			@Override
			public void onFailure(Throwable caught) {
				
			}

			@Override
			public void onSuccess(Integer result) {
				holidayUnit = result;
				refresh();
			} } );
//		listsLoaded();
	}
	public void listsLoaded()	{
		retrieveDTOs();
	}
	public void setDate(Date date)	{
		storedDate = date;
		if (holidayUnit!=null)
			refresh();
	}
	public void retrieveDTOs()	{
		greetingService.retrieveTimeTableTasks(false, false, false, false, true, storedDate, new AsyncCallback<List<TimeTableTaskDTO>>()  {
			@Override
			public void onFailure(Throwable caught) {
				log.info("WeeklyTimeTableTable: " + caught.toString());
			}

			@Override
			public void onSuccess(List<TimeTableTaskDTO> result) {
				loadData(convertDTOtoRowArray(result));
			}
		});		
	}

	private List<List<Object>> convertDTOtoRowArray(List<TimeTableTaskDTO> timeTableTaskDTOs)	{
		List<List<Object>> rows = new ArrayList<List<Object>>();
		for (TimeTableTaskDTO dto:timeTableTaskDTOs)	{
			List<Object> row = new ArrayList<Object>();
			row.add(dto.getId());
			row.add(dto.getHolidayUnit());
			row.add(dto.getHolidayDate());
			row.add(dto.getTask()!=null?dto.getTask():"");
			rows.add(row);
		}
		return rows;
	}
	protected void addObject(int row, int column, Object object)	{
		ensureRowAvailable(row);
		GenericColumn genericColumn = genericColumns.getColumn(column);
//		if (genericColumn.getType().equals(JobSiteDTO.class))	{
//			addJobSiteDTO(row, column, object, genericColumn);
	}
	
	public void updateItemCell(int row)	{
		TimeTableTaskDTO dto = new TimeTableTaskDTO();
		dto.setId(getId(row));
		dto.setHolidayUnit(getInteger(row, 1));
		dto.setHolidayDate(getDate(row, 2));
		dto.setTask(getText(row, 3));
		dto.setDate(storedDate);
		updateRow(dto);
	}

	private void updateRow(TimeTableTaskDTO dto)	{
		try {
			greetingService.updateTimeTableTask(dto, new AsyncCallback<String>()	{
					@Override
					public void onFailure(Throwable caught) {
						log.info("YearlyTimeTableTable: " + caught.toString());
					}

					@Override
					public void onSuccess(String result) {
						setRowIdAfterUpdate(result);
					}
				});
		} catch (IllegalArgumentException e) {
			log.info("HealthAlertTable: " + e.toString());
		} /*catch (SecurityAccessDeniedException e) {
			log.info("SecurityAccessDeniedException: " + e.toString());
		} 		*/
	}
}
